package com.wang.gulimall.member.controller;

import com.wang.common.utils.PageUtils;
import com.wang.common.utils.R;
import com.wang.gulimall.member.entity.MemberReceiveAddressEntity;
import com.wang.gulimall.member.service.MemberReceiveAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 会员收货地址
 *
 * @author wangliuyang
 * @email 1842184157@qq.com
 * @date 2021-01-12 18:58:19
 */
@RestController
@RequestMapping("member/memberreceiveaddress")
@Slf4j
public class MemberReceiveAddressController {

    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;

    /**
     * 根据会员id查询会员的所有地址  可以用
     *
     * @param memberId
     * @return
     */
    @GetMapping("/{memberId}/address")
    public List<MemberReceiveAddressEntity> getMemberAddressList(@PathVariable("memberId") Long memberId) {
        log.error("调用会员服务查询地址");
        List<MemberReceiveAddressEntity> addressEntities = memberReceiveAddressService.getMemberAddressList(memberId);
        return addressEntities;
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberReceiveAddressEntity memberReceiveAddress = memberReceiveAddressService.getById(id);
        return R.ok().put("memberReceiveAddress", memberReceiveAddress);
    }


    /**
     * 根据会员id查询会员的所有地址
     *
     * @param memberId
     * @return
     */
    @GetMapping(value = "/info/getAddress/{id}")
    public List<MemberReceiveAddressEntity> getAddress(@PathVariable("memberId") Long memberId) {
        log.error("调用会员服务查询地址");
        List<MemberReceiveAddressEntity> addressList = memberReceiveAddressService.getAddress(memberId);
        return addressList;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberReceiveAddressService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberReceiveAddressEntity memberReceiveAddress) {
        memberReceiveAddressService.save(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberReceiveAddressEntity memberReceiveAddress) {
        memberReceiveAddressService.updateById(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberReceiveAddressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
